home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap11 / Colors2 / Colors2.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  4.5 KB  |  136 lines

  1. /*------------------------------------------------
  2.    COLORS2.C -- Version using Modeless Dialog Box
  3.                 (c) Charles Petzold, 1998
  4.   ------------------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. LRESULT CALLBACK WndProc     (HWND, UINT, WPARAM, LPARAM) ;
  9. BOOL    CALLBACK ColorScrDlg (HWND, UINT, WPARAM, LPARAM) ;
  10.  
  11. HWND hDlgModeless ;
  12.  
  13. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  14.                     PSTR szCmdLine, int iCmdShow)
  15. {
  16.      static TCHAR szAppName[] = TEXT ("Colors2") ;
  17.      HWND         hwnd ;
  18.      MSG          msg ;
  19.      WNDCLASS     wndclass ;
  20.      
  21.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  22.      wndclass.lpfnWndProc   = WndProc ;
  23.      wndclass.cbClsExtra    = 0 ;
  24.      wndclass.cbWndExtra    = 0 ;
  25.      wndclass.hInstance     = hInstance ;
  26.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  27.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  28.      wndclass.hbrBackground = CreateSolidBrush (0L) ;
  29.      wndclass.lpszMenuName  = NULL ;
  30.      wndclass.lpszClassName = szAppName ;
  31.      
  32.      if (!RegisterClass (&wndclass))
  33.      {
  34.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  35.                       szAppName, MB_ICONERROR) ;
  36.           return 0 ;
  37.      }
  38.      
  39.      hwnd = CreateWindow (szAppName, TEXT ("Color Scroll"),
  40.                           WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
  41.                           CW_USEDEFAULT, CW_USEDEFAULT,
  42.                           CW_USEDEFAULT, CW_USEDEFAULT,
  43.                           NULL, NULL, hInstance, NULL) ;
  44.      
  45.      ShowWindow (hwnd, iCmdShow) ;
  46.      UpdateWindow (hwnd) ;
  47.      
  48.      hDlgModeless = CreateDialog (hInstance, TEXT ("ColorScrDlg"), 
  49.                                   hwnd, ColorScrDlg) ;
  50.      
  51.      while (GetMessage (&msg, NULL, 0, 0))
  52.      {
  53.           if (hDlgModeless == 0 || !IsDialogMessage (hDlgModeless, &msg))
  54.           {
  55.                TranslateMessage (&msg) ;
  56.                DispatchMessage  (&msg) ;
  57.           }
  58.      }
  59.      return msg.wParam ;
  60. }
  61.  
  62. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  63. {
  64.      switch (message)
  65.      {
  66.      case WM_DESTROY :
  67.           DeleteObject ((HGDIOBJ) SetClassLong (hwnd, GCL_HBRBACKGROUND,
  68.                               (LONG) GetStockObject (WHITE_BRUSH))) ;
  69.           PostQuitMessage (0) ;
  70.           return 0 ;
  71.      }
  72.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  73. }
  74.  
  75. BOOL CALLBACK ColorScrDlg (HWND hDlg, UINT message, 
  76.                            WPARAM wParam, LPARAM lParam)
  77. {
  78.      static int iColor[3] ;
  79.      HWND       hwndParent, hCtrl ;
  80.      int        iCtrlID, iIndex ;
  81.      
  82.      switch (message)
  83.      {
  84.      case WM_INITDIALOG :
  85.           for (iCtrlID = 10 ; iCtrlID < 13 ; iCtrlID++)
  86.           {
  87.                hCtrl = GetDlgItem (hDlg, iCtrlID) ;
  88.                SetScrollRange (hCtrl, SB_CTL, 0, 255, FALSE) ;
  89.                SetScrollPos   (hCtrl, SB_CTL, 0, FALSE) ;
  90.           }
  91.           return TRUE ;
  92.           
  93.      case WM_VSCROLL :
  94.           hCtrl   = (HWND) lParam ;
  95.           iCtrlID = GetWindowLong (hCtrl, GWL_ID) ;
  96.           iIndex  = iCtrlID - 10 ;
  97.           hwndParent = GetParent (hDlg) ;
  98.           
  99.           switch (LOWORD (wParam))
  100.           {
  101.           case SB_PAGEDOWN :
  102.                iColor[iIndex] += 15 ;        // fall through
  103.           case SB_LINEDOWN :
  104.                iColor[iIndex] = min (255, iColor[iIndex] + 1) ;
  105.                break ;
  106.           case SB_PAGEUP :
  107.                iColor[iIndex] -= 15 ;        // fall through
  108.           case SB_LINEUP :
  109.                iColor[iIndex] = max (0, iColor[iIndex] - 1) ;
  110.                break ;
  111.           case SB_TOP :
  112.                iColor[iIndex] = 0 ;
  113.                break ;
  114.           case SB_BOTTOM :
  115.                iColor[iIndex] = 255 ;
  116.                break ;
  117.           case SB_THUMBPOSITION :
  118.           case SB_THUMBTRACK :
  119.                iColor[iIndex] = HIWORD (wParam) ;
  120.                break ;
  121.           default :
  122.                return FALSE ;
  123.           }
  124.           SetScrollPos  (hCtrl, SB_CTL,      iColor[iIndex], TRUE) ;
  125.           SetDlgItemInt (hDlg,  iCtrlID + 3, iColor[iIndex], FALSE) ;
  126.           
  127.           DeleteObject ((HGDIOBJ) SetClassLong (hwndParent, GCL_HBRBACKGROUND,
  128.                               (LONG) CreateSolidBrush (
  129.                                    RGB (iColor[0], iColor[1], iColor[2])))) ;
  130.           
  131.           InvalidateRect (hwndParent, NULL, TRUE) ;
  132.           return TRUE ;
  133.      }
  134.      return FALSE ;
  135. }
  136.